<template>
  <div class="monitoring-records-container">
    <el-table :data="records" border style="width: 100%">
      <el-table-column prop="studentName" label="考生" width="120">
        <template slot-scope="{row}">
          <el-avatar :size="30" :src="row.studentAvatar" />
          <span style="margin-left: 10px;">{{ row.studentName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="eventType" label="异常类型" width="120">
        <template slot-scope="{row}">
          <el-tag :type="row.eventType | eventTypeFilter">
            {{ row.eventType | eventTypeTextFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="eventTime" label="发生时间" width="180" />
      <el-table-column prop="screenshot" label="截图证据">
        <template slot-scope="{row}">
          <el-image
            v-if="row.screenshot"
            style="width: 100px; height: 60px"
            :src="row.screenshot"
            :preview-src-list="[row.screenshot]"
            fit="cover"
          />
          <span v-else>无截图</span>
        </template>
      </el-table-column>
      <el-table-column prop="handleResult" label="处理结果" />
      <el-table-column label="操作" width="120">
        <template slot-scope="{row}">
          <el-button type="text" @click="handleViewDetail(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="query.page"
      :limit.sync="query.limit"
      @pagination="getRecords"
    />
  </div>
</template>

<script>
import { fetchMonitoringRecords } from '@/api/exam/monitoring'
import Pagination from '@/components/Pagination'

export default {
  name: 'MonitoringRecords',
  components: { Pagination },
  props: {
    examId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      records: [],
      total: 0,
      query: {
        page: 1,
        limit: 10
      }
    }
  },
  watch: {
    examId: {
      immediate: true,
      handler() {
        this.getRecords()
      }
    }
  },
  methods: {
    async getRecords() {
      try {
        const { data } = await fetchMonitoringRecords({
          examId: this.examId,
          ...this.query
        })
        this.records = data.items
        this.total = data.total
      } catch (error) {
        console.error(error)
      }
    },

    handleViewDetail(row) {
      // 查看详情逻辑
    }
  },
  filters: {
    eventTypeFilter(type) {
      const map = {
        1: 'danger',   // 切屏
        2: 'warning',  // 人脸不符
        3: 'info'      // 其他异常
      }
      return map[type] || ''
    },
    eventTypeTextFilter(type) {
      const map = {
        1: '切屏警告',
        2: '人脸不符',
        3: '其他异常'
      }
      return map[type] || ''
    }
  }
}
</script>

<style scoped>
.monitoring-records-container {
  padding: 20px;
}
</style>
